package com.pxxy.yuyi.controller;

import cn.dsna.util.images.ValidateCode;
import com.github.pagehelper.PageInfo;
import com.pxxy.yuyi.entity.Admin;
import com.pxxy.yuyi.service.AdminService;
import com.pxxy.yuyi.utils.ConstUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 *
 * @Author : XuWen
 * @create 2021/6/21 11:08
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private AdminService adminService;
    //管理员信息展示
    @RequestMapping("/show")
    public String show(HttpServletRequest request,Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        //1.先要判断是否有登录，如果登录了，则直接操作加入购物车
        //没有登录，则先进行登录
        Admin admin = (Admin) request.getSession().getAttribute(ConstUtils.LOGIN_USER);
        if (admin==null) {//为空则先进行登录
            request.setAttribute("msg", "请先登录");
            return "admin/adminLogin";
        }else {
            PageInfo<Admin> info =adminService.selectAll(pageNum);

            request.setAttribute("pi",info);
            return "admin/index";
        }
    }
    //删除管理员
    @RequestMapping("/deleteAdmin")
    public String delteAdmin(HttpSession session){
        Admin user= (Admin) session.getAttribute(ConstUtils.LOGIN_USER);
        Integer result = adminService.deleteAdmin(user.getId());
        return "forward:/admin/show";
    }
    //添加管理员
    @RequestMapping("/addAdmin")
    public String addAdmin(Admin admin){
        Integer result = adminService.addAdmin(admin);
        return "forward:/admin/show";
    }
    //修改管理员
    @RequestMapping("/updateAdmin")
    public String updateAdmin(Admin admin){
        System.out.println("2222"+admin.getId());
        Integer result = adminService.updateAdmin(admin);
        return "forward:/admin/show";
    }

    //验证码生成
    @RequestMapping("/validCode")
    public void vaildCode(HttpServletResponse response, HttpSession session) throws IOException {
        ValidateCode validateCode = new ValidateCode(100,35,4,8);
        System.out.println("验证码-->"+validateCode.getCode());
        session.setAttribute("imgCode",validateCode.getCode());
        //渲染到前端页面
        validateCode.write(response.getOutputStream());
    }
    //登陆
    @RequestMapping("/login")
    public String login(Admin admin, HttpServletRequest request,String vcode,String auto,HttpServletResponse response){
        //获取图片验证
        String imgCode= (String) request.getSession().getAttribute("imgCode");
        if (!imgCode.equalsIgnoreCase(vcode)){
            request.setAttribute("msg","验证码错误");
            return "admin/adminLogin";
        }

        Admin user1 = adminService.selectLogin(admin.getName(),admin.getPassword());
        if (user1==null){
            request.setAttribute("msg","用户名或密码错误");
            return "admin/adminLogin";
        }else {
            //设置cookie
            Cookie cookie = new Cookie(ConstUtils.AUTO_LOGIN,admin.getName()+":"+admin.getPassword());
            if (auto==null){
                cookie.setMaxAge(0);//没有选择，即为清除cookie
            }else {
                cookie.setMaxAge(60*60*24*14);//存储有效期，单位为秒
                response.addCookie(cookie);
            }
            System.out.println("登录成功");
            request.getSession().setAttribute(ConstUtils.LOGIN_USER,user1);
        }
        return "forward:/admin/show";
    }
    //自动登陆凭证
    @RequestMapping("/auto")
    public String auto(){
        return "admin/adminLogin";
    }

    //退出
    @RequestMapping("/logout")
    public String logout(HttpServletResponse response,HttpSession session){
        //1.清楚cookie
        Cookie cookie = new Cookie(ConstUtils.AUTO_LOGIN,"xxx");
        cookie.setMaxAge(0);//清楚cookie
        response.addCookie(cookie);
        //2.清楚session登录凭证
        session.removeAttribute(ConstUtils.LOGIN_USER);
        return "index";
    }


}
